// Promise封装请求   options是一个对象里面的属性有url, data, method, load
//url 对应请求路径  data对象请求的参数  method对应请求方法  load对应是否显示加载中 1显示 0不显示
let util = require('./storage');
let getLon = require('../utils/js/WSCoordinate')
let app = getApp()
let modelStatus = false;

function httpReq(options) {
    let title = '加载中'
    if (options.title) {
        title = options.title
    }
    if (options.load == 1) {
        wx.showLoading({
            mask: true,
            title: title
        })
    }
    return new Promise((resolve, reject) => {
        // 所有的请求加上固定的参数  区分是简单请求还是特殊请求
        //如果options.data是对象 表示是简单请求

        if (Object.prototype.toString.call(options.data) === '[Object Object]') {
        }
        //如果是json字符串的是特殊请求
        if (Object.prototype.toString.call(options.data) === "[object String]") {
            let obj = JSON.parse(options.data)
            options.data = JSON.stringify(obj)
        }
        let token = util.get('APPToken')
        // let token = ''
        let header;
        let getSystemInfoSync = wx.getSystemInfoSync()
        let DeviceInfo = 'mini_' + getSystemInfoSync.brand + '_' + getSystemInfoSync.model + '_' + getSystemInfoSync.system + '_WechatVersion' + getSystemInfoSync.version + '_BaselibraryVersion' + getSystemInfoSync.SDKVersion
        if (token) {

            header = {
                "content-type": "application/x-www-form-urlencoded",
                "Authorization": token,
                "PlatformIdentification": "wechatApplet",
                "DeviceInfo": DeviceInfo
            }

        } else {
            header = {
                "content-type": "application/x-www-form-urlencoded",
                "PlatformIdentification": "wechatApplet",
                "DeviceInfo": DeviceInfo
            }
        }

        wx.request({
            url: options.url,
            data: options.data,
            header: header,
            method: options.method,
            complete: function (gcr) {
                wx.hideLoading();
            },
            success: function (res) {
                if (res.data.StateCode == 0) {
                    resolve(res.data); //把请求到的数据发到引用请求的地方
                } else if (res.data.StateCode == 40001) {

                    if (options.showModal !== 0) {
                        wx.showModal({
                            title: '温馨提示',
                            content: res.data.Msg,
                            showCancel: false,
                            confirmColor: "#008CD3",
                            success(res1) {
                                if (res1.confirm) {
                                    resolve(res.data);
                                }
                            }
                        })
                    }else{
                        resolve(res.data);
                    }
                    
                } else if (res.data.StateCode == 40002) {
                    if (options.showToast !== 0) {
                        wx.showToast({
                            title: res.data.Msg,
                            icon: 'success',
                            duration: 2000,
                            success: res => {
                                util.clear()
                                wx.reLaunch({
                                    url: '/pages/login/index?type=' + '1',
                                })
                            }
                        })
                    }else{
                        resolve(res.data);
                    }
                }
            },
            fail: function (err) {
                reject(err)
            }
        })
    });
}
// 上传图片
function SelectFile(options) {

    return new Promise((resolve, reject) => {
        wx.chooseImage({
            count: options.count, // 最多可以选择的图片张数，默认9
            sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
            sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
            success: res => {
                let tempFilePaths = res.tempFilePaths
                let token = util.get('APPToken')
                if (options.load == 1) {
                    wx.showLoading({
                        mask: true,
                        title: '上传中'
                    })
                }
                let getSystemInfoSync = wx.getSystemInfoSync()
                let DeviceInfo = 'mini_' + getSystemInfoSync.brand + '_' + getSystemInfoSync.model + '_' + getSystemInfoSync.system + '_WechatVersion' + getSystemInfoSync.version + '_BaselibraryVersion' + getSystemInfoSync.SDKVersion
                wx.uploadFile({
                    url: options.url,
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        "content-type": "application/x-www-form-urlencoded",
                        "Authorization": token,
                        "PlatformIdentification": "wechatApplet",
                        "DeviceInfo": DeviceInfo
                    }, // 设置请求的 header
                    formData: options.data,
                    complete: function (gcr) {
                        wx.hideLoading();
                    },
                    success: upRes => {
                        resolve(upRes); //把请求到的数据发到引用请求的地方
                    },
                    fail: err => {
                        reject(err)
                    },
                })
            }
        })
    })
}

// 上传打卡图片
function SelectFile1(options) {
    return new Promise((resolve, reject) => {
        wx.chooseImage({
            count: options.count, // 最多可以选择的图片张数，默认9
            sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
            sourceType: ['camera'], // album 从相册选图，camera 使用相机，默认二者都有
            success: res => {
                let tempFilePaths = res.tempFilePaths
                let token = util.get('APPToken')
                wx.uploadFile({
                    url: options.url,
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        "content-type": "application/x-www-form-urlencoded",
                        "Authorization": token,
                        "PlatformIdentification": "wechatApplet"
                    }, // 设置请求的 header
                    formData: options.data,
                    success: upRes => {
                        resolve(upRes); //把请求到的数据发到引用请求的地方
                    },
                    fail: err => {
                        reject(err)
                    },
                })
            }
        })
    })
}
// 上传视频
function videofile(options) {
    return new Promise((resolve, reject) => {
        let token = util.get('APPToken')
        wx.uploadFile({
            url: options.url,
            filePath: options.data,
            name: 'file',
            header: {
                "content-type": "application/x-www-form-urlencoded",
                "Authorization": token,
                "PlatformIdentification": "wechatApplet"
            }, // 设置请求的 header
            success: upRes => {
                resolve(upRes); //把请求到的数据发到引用请求的地方
            },
            fail: err => {
                reject(err)
            },
        })
    })
}

module.exports = {
    httpReq,
    SelectFile,
    videofile,
    SelectFile1,

}